package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.PetDao;
import com.njtech.pet.entity.Pet;
import com.njtech.pet.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Calendar;
import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-13 17:33
 * Description 测试PetDao
 */
public class PetDaoExample {
    static PetDao petDao;
    static Scanner scanner;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        petDao = (PetDao) context.getBean("petDao");

        System.out.println("准备开始测试" + petDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit,");
        System.out.println("1: test insert,");
        System.out.println("2: test update,");
        System.out.println("3: test select.");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }
    }

    private static void insert() {
        Pet pet = new Pet();
        pet.setName("小咪");
        pet.setSpecies("猫");

        Calendar c1 = Calendar.getInstance();
        //2014-5-9日
        c1.set(2018, 5 - 1, 9);
        pet.setBirthday(c1.getTime());

        pet.setLisence("lisence");
        pet.setImmune("immune");
        pet.setColor("red");
        pet.setSex("MALE");
        pet.setBirthControl("yes");
        pet.setChip("chip");
        pet.setState(Constant.Pet.STATE_VALID.getValue());
        pet.setRemark("remark");

        User user = new User();

        System.out.println("please enter user id:");
        int id = scanner.nextInt();
        user.setId(id);
        pet.setUser(user);
        System.out.println("inserting...");
        petDao.insert(pet);
        System.out.println("insert finished please check database");
    }

    private static void update() {
        Pet pet = new Pet();
        pet.setName("小狗");

        Calendar c = Calendar.getInstance();
        //2014-5-9日
        c.set(2020, 7 - 1, 9);
        pet.setBirthday(c.getTime());


        pet.setBirthControl("no");

        pet.setState(Constant.Pet.STATE_INVALID.getValue());
        pet.setRemark("23333");

        int id;
        System.out.println("please enter pet id:");
        id = scanner.nextInt();
        pet.setId(id);

        System.out.println("updating...");
        petDao.updateByPetId(pet);
        System.out.println("update finished please check database");

    }

    private static void select() {
        User user = new User();
        System.out.println("please enter user id:");
        int id = scanner.nextInt();
        user.setId(id);
        List<Pet> pets;
        System.out.println("test select with state valid");
        pets = petDao.selectByUserId(user);
        for (Pet pet:pets) {
            System.out.println(pet);
        }

    }
}
